package com.mdlaser.util;

import cn.dev33.satoken.stp.StpUtil;
import com.mdlaser.sys.entity.User;
import com.mdlaser.sys.service.IUserService;

public class UserUtil {
    static IUserService userService;

    static {
        userService = SpringBeanUtil.getBean(IUserService.class);
    }

    public static User getCurrentUser() {
        String loginId;
        loginId = (String) StpUtil.getLoginId();
        return userService.getUserById(loginId);
    }


    public static User getCurrentUserSafe() {
        String loginId;
        try {
            loginId = (String) StpUtil.getLoginId();
        } catch (Exception e) {
            return null;
        }
        return userService.getUserById(loginId);
    }

    public static String getCurrentUserId() {
        String loginId = (String) StpUtil.getLoginId();
        User user = userService.getUserById(loginId);
        if (user != null) {
            return user.getId();
        }
        return null;
    }
}
